package cn.zh.stan.activity;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import cn.zh.stan.BaseUrl;
import cn.zh.stan.R;
import cn.zh.stan.util.ToastUtil;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class FindPth extends BaseActivity {

    ImageView psthback;

    ImageView tpimag,twimg;

    LinearLayout tphlin,tpslin;

    EditText tphone,tpass;

    Button reset;

    String uid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_pth);
        uid=getIntent().getStringExtra("uid");
        intvth();
         }


    public void intvth(){
        psthback= (ImageView) findViewById(R.id.psthback);
        psthback.setOnClickListener(this);

        tphone= (EditText) findViewById(R.id.tphone);
        tpass= (EditText) findViewById(R.id.tpass);
        tpimag= (ImageView) findViewById(R.id.tpimag);
        twimg= (ImageView) findViewById(R.id.twimg);
        tphlin= (LinearLayout) findViewById(R.id.tphlin);
        tpslin= (LinearLayout) findViewById(R.id.tpslin);
        reset= (Button) findViewById(R.id.reset);

        tpimag.setOnClickListener(this);
        twimg.setOnClickListener(this);
        reset.setOnClickListener(this);

        tphone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void afterTextChanged(Editable editable) {
                int leng=editable.length();
                if (leng>0){
                    tpimag.setVisibility(View.VISIBLE);
                    tphlin.setBackgroundResource(R.drawable.cprimbg);
                }else {
                    tpimag.setVisibility(View.INVISIBLE);
                    tphlin.setBackgroundResource(R.drawable.graybg);
                }
            }
        });

        tpass.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void afterTextChanged(Editable editable) {
                int leng=editable.length();
                if (leng>0){
                    twimg.setVisibility(View.VISIBLE);
                    tpslin.setBackgroundResource(R.drawable.cprimbg);
                }else {
                    twimg.setVisibility(View.INVISIBLE);
                    tpslin.setBackgroundResource(R.drawable.graybg);
                }
            }
          });
       }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id= view.getId();
        switch (id){
            case R.id.psthback:
                finish();
                break;
            case R.id.pimag:
                tphone.getText().clear();
                tphlin.setBackgroundResource(R.drawable.graybg);
                tpimag.setVisibility(View.INVISIBLE);
                break;
            case R.id.wimg:
                tpass.getText().clear();
                tpslin.setBackgroundResource(R.drawable.graybg);
                twimg.setVisibility(View.INVISIBLE);
                break;
            case R.id.reset:
               String ops=tphone.getText().toString();
               String tps=tpass.getText().toString();
               if (ops.equals(tps)){
                   getPost(ops);
               }else {
                   ToastUtil.showShort(FindPth.this,"前后密码不一致");

               }
                //修改密码
                break;
             }
           }

           //修改密码
           public void getPost(String pass){
               OkHttpUtils.post(BaseUrl.pass_up)
                       .params("userId",uid)
                       .params("password",pass)
                       .execute(new StringCallback() {
                           @Override
                           public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                               System.out.println("------------------>修改密码====="+s);
                               try {
                                   JSONObject js=new JSONObject(s);
                                   boolean State=js.getBoolean("State");
                                   if (State){
                                       boolean Flag=js.getBoolean("Flag");
                                       if (Flag){
                                           Intent intent=new Intent(FindPth.this,Login.class);
                                           startActivity(intent);
                                           String Msg=js.getString("Msg");
                                           ToastUtil.showLong(FindPth.this,Msg);
                                           finish();
                                       }else {
                                         String Msg=js.getString("Msg");
                                         ToastUtil.showLong(FindPth.this,Msg);
                                       }
                                   }

                               } catch (JSONException e) {
                                   e.printStackTrace();
                               }
                           }
                           @Override
                           public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                               super.onError(isFromCache, call, response, e);
                           }
                       });



             }





}
